<?php

	require("includes/database.php");

	$inNroLinea = $_GET[nroLinea];
	$inNroImei = $_GET[nroImei];
	$inNroOrden = $_GET[nroOrden];
	
	$mySQL = "SELECT O.idOrden, O.fechaHoraInicio as fecha, A.nombre FROM ordenes O left join almacenes A on O.idAlmacen = A.idAlmacen WHERE 1=1";
	if($inNroImei != "")
	{
		$mySQL = $mySQL . " AND O.imei='" . $inNroImei . "'";
		$mySQL = $mySQL . " OR O.imeiSalida='" . $inNroImei . "'";
		$mySQL .= " union all select 'Envio de SWAP', s.fechaEntrega as fecha, concat('Cliente: ',c.nombre)as cliente from stock s join clientes c on s.idCliente = c.idCliente where s.imei = '$inNroImei' and fecha <> '0000-00-00' order by fecha desc";
		
	}
	if($inNroLinea != "")
		$mySQL = $mySQL . " AND O.nroLinea='" . $inNroLinea . "'";
	if($inNroOrden != "")
		$mySQL = $mySQL . " AND O.idOrden=" . $inNroOrden;
	
	//echo $mySQL;
	
	$result = readSQL($mySQL);
	if(count($result) != 0)
	{
	?>
	<table style="width:100%;">
		<tr>
			<th class="listaHead" colspan="4">Historial</th>
		</tr>
		<tr>
			<th class="listaHead" width="50%">Nro.Orden</th>
			<th class="listaHead" width="50%">Fecha</th>
            <th class="listaHead" width="50%">Deposito</th>
		</tr>
		<?php
			$i = 0;
			foreach($result as $value)
			{
				if($i%2==0)
	        		$class = "par";
	        	else
	        		$class = "impar";
	        		
	    		list($ano, $mes, $dia) =  split("-", substr($value['fecha'], 0, 10));
				$fecha = $dia . "/" . $mes . "/" . $ano;
		?>
		<tr>
			<td class="<?php echo $class;?>" align="right">
            	<?php
				if($value['idOrden'] != "Envio de SWAP")
				{
				?>
            		<a target="_blank" href="verOrden.php?iOrden=<?php echo $value['idOrden'];?>" class="listaHeadLink">
                    	<?php echo $value['idOrden'];?>
                    </a>
                <?php
				}
				else
				{
					echo "<b>".$value['idOrden']."</b>";
				}
				?>
            </td>
			<td class="<?php echo $class;?>" align="right">
            	<?php
				if($value['idOrden'] != "Envio de SWAP")
				{
				?>
            		<a target="_blank" href="verOrden.php?iOrden=<?php echo $value['idOrden'];?>" class="listaHeadLink">
						<?php echo $fecha;?>
                    </a>
                <?php
				}
				else
				{
					echo "<b>".$fecha."</b>";
				}
				?>
             </td>
            <td class="<?php echo $class;?>" align="right">
            	<?php
				if($value['idOrden'] != "Envio de SWAP")
				{
				?>
            		<a target="_blank" href="verOrden.php?iOrden=<?php echo $value['idOrden'];?>" class="listaHeadLink">
						<?php echo $value['nombre'];?>
                    </a>
                <?php
				}
				else
				{
					echo "<b>".$value['nombre']."</b>";
				}
				?>
            </td>
		</tr>
		<?php
				$i++;
			}
		?>
	</table>
	<?php
	}
	else
		echo "<font color='red'><b>No hay informacion para los criterios ingresados.</b></font>";
?>